<?php
namespace Model;



use Core\Exception\SystemException;
use Exception;
use Model\Factory\TableGatewayFactory;

class TableGatewayTransactionProxy
{

    protected  $abstractTable;


    public function setAbstractTable($abstractTable)
    {
        $this->abstractTable = $abstractTable;
    }


    public function getAbstractTable()
    {
        return $this->abstractTable;
    }


    public function __call($method, $args) {
        $abstractTable = $this->getAbstractTable();
        if(!$abstractTable) throw new SystemException('Chưa đăng ký abstract table cho transaction proxy');
        $abstractTable->getTableGateway()->getAdapter()->getDriver()->getConnection()->beginTransaction();
        try{
            call_user_func_array(array($abstractTable, $method), $args);
            $abstractTable->getTableGateway()->getAdapter()->getDriver()->getConnection()->commit();
        }catch (Exception $e){
            $abstractTable->getTableGateway()->getAdapter()->getDriver()->getConnection()->rollback();
            throw $e;
        }


    }
}
